package com.haihui.platform.controller.sys;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.haihui.platform.controller.BaseController;
import com.haihui.platform.entity.SysDictionary;
import com.haihui.platform.service.sys.SysDictionaryService;
import com.haihui.platform.utils.PrimaryKeyGen;

/*
 * 字典表
 */
@Controller
@RequestMapping("/dic")
public class SysDictionaryController extends BaseController{

	private static final Logger LOG = Logger.getLogger(SysDictionaryController.class);
	
	@Autowired
	private SysDictionaryService sysDictionaryService;
	
	/**
	 * 根据类型获得字典
	 * @param dic
	 */
	@RequestMapping(value="/getDic")
	public void dic(SysDictionary dic){
		try{
			List<SysDictionary> list = sysDictionaryService.dic(dic);
			this.responseWriter(this.getCommonJsonString(true, "查询成功", list));
		}catch(Exception e){
			LOG.error(e.getMessage(),e);
			this.responseWriter(this.getCommonJsonString(false, "查询失败"));
		}
	}
	
	/**
	 * 新增字典
	 * @param dic
	 */
	@RequestMapping(value="/add")
	public void add(SysDictionary dic){
		try{
			dic.setDicId(PrimaryKeyGen.getKey());
			int count = sysDictionaryService.add(dic);
			this.responseWriter(this.getCommonJsonString(true, "新增成功", count));
		}catch(Exception e){
			LOG.error(e.getMessage(),e);
			this.responseWriter(this.getCommonJsonString(false, "新增失败"));
		}
	}
	
	/**
	 * 字典列表
	 */
	@RequestMapping(value="/list")
	public void list(){
		try{
			List<SysDictionary> list = sysDictionaryService.list();
			this.responseWriter(this.getCommonJsonString(true, "查询成功", list));
		}catch(Exception e){
			LOG.error(e.getMessage(),e);
			this.responseWriter(this.getCommonJsonString(false, "查询失败"));
		}
	}
	
	/**
	 * 删除字典
	 * @param dic
	 */
	@RequestMapping(value="/delete")
	public void delete(SysDictionary dic){
		try{
			int count = sysDictionaryService.delete(dic);
			this.responseWriter(this.getCommonJsonString(true, "删除成功", count));
		}catch(Exception e){
			LOG.error(e.getMessage(),e);
			this.responseWriter(this.getCommonJsonString(false, "删除失败"));
		}
	}
	
	/**
	 * 修改字典
	 * @param dic
	 */
	@RequestMapping(value="/update")
	public void update(SysDictionary dic){
		try{
			int count = sysDictionaryService.update(dic);
			this.responseWriter(this.getCommonJsonString(true, "修改成功", count));
		}catch(Exception e){
			LOG.error(e.getMessage(),e);
			this.responseWriter(this.getCommonJsonString(false, "修改失败"));
		}
	}
	
}
